package ebedynky.controllers;

import ebedynky.beans.Grange;
import ebedynky.services.GrangeService;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Milanec
 */
@Controller
public class GrangeController {

    /**
     * Fetches the Grange adding page when requested.
     * @param model data holder for this view
     * @return string representing page location
     */
    @RequestMapping(value="/grange", method=RequestMethod.GET)
        public String getCreateForm(Model model) {
            model.addAttribute(new Grange());
            return "grange/grange";
        }

    /**
     * Extracts the Grange data from the form,
     * validates them and stores them into the db.
     * @param grange data from the form
     * @param result data binding result
     * @return string representing next (success/failure) page location
     */
    @RequestMapping(value="/grange", method=RequestMethod.POST)
        public String create(@Valid Grange grange, BindingResult result) {
            if (result.hasErrors()) {
                System.out.println("Errrooor");
                return "grange/grange";
            }
            GrangeService gs = new GrangeService();
            gs.addGrange(grange);
            return "/grange/success";
        }

}
